package com.example.libpro.MyActivity.adminActivity;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.libpro.DBEntity.BookEntity;
import com.example.libpro.MyActivity.adminAdapter.searchBookAdapter;
import com.example.libpro.R;
import com.example.libpro.Service.BookService;

import java.util.List;

public class AdminSearchBookActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "AdminSearchBookActivity";
    private BookService bookService;
    private boolean bookServiceBound;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            BookService.LocalBinder binder = (BookService.LocalBinder) service;
            bookService = binder.getService();
            bookServiceBound = true;
        }
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            bookServiceBound = false;
        }
    };
    private Button btnSearchBook;
    private EditText etBookName;
    private RecyclerView rvBook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_search_book);
        Log.i(TAG, "onCreate: Activity 启动，开始绑定 bookService");
        Intent intent = new Intent(this, BookService.class);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);

        etBookName=findViewById(R.id.etSearch);
        btnSearchBook=findViewById(R.id.btnSearch);
        rvBook=findViewById(R.id.rvFree);

        btnSearchBook.setOnClickListener(this);



    }

    @Override
    public void onClick(View view) {
        //判断绑定状态和各个控件的输入
        Log.d(TAG, "查询图书");
        boolean isOk = bookServiceBound;
        if (isOk) {
            List<BookEntity> books = bookService.queryBooks(etBookName.getText().toString(), null, null, true).getBookList();
            // 初始化查询图书
            searchBookAdapter adapter = new searchBookAdapter(books);
            // 设置子控件点击事件
            adapter.setOnItemChildClickListener((adapter1, view1, position) -> {
                Log.d(TAG, "点击了位置: " + position + ", View ID: " + view1.getId());
                if (view1.getId() == R.id.itembook) { // 假设 R.id.itembook 是触发弹窗的按钮
                    BookEntity book = books.get(position);
                    Intent AdminSearchBookItemActivityintent = new Intent(AdminSearchBookActivity.this, AdminSearchBookItemActivity.class);
                    AdminSearchBookItemActivityintent.putExtra("book", book);
                    startActivity(AdminSearchBookItemActivityintent);
                }
            });
            rvBook.setAdapter(adapter);
            rvBook.setLayoutManager(new LinearLayoutManager(adapter.getContext()));
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: Activity 停止，开始解绑 bookService");
        unbindService(connection);
        bookServiceBound = false;
        bookService = null;
        Toast.makeText(this, "已解绑 bookService", Toast.LENGTH_SHORT).show();
        Log.i(TAG, "onStop: Activity 停止，已解绑 bookService");
        finish();
    }
}